﻿#include "searchhotelheader.h"
#include "ui_searchhotelheader.h"
#include <QDebug>
#include <QMessageBox>
#include <QStringBuilder>

/// m_findCondition  [type,textIndex,text]
/// type     city      outTime      star     name     location mark    price    inTime
SearchHotelHeader::SearchHotelHeader(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SearchHotelHeader)
{
    ui->setupUi(this);

    m_hotelTypeList.clear();
    m_hotelStarList.clear();

    on_pb_typeNoCondition_clicked();
    on_pb_starNoCondition_clicked();
    on_pb_priceNoCondition_clicked();
    on_pb_markNoCondition_clicked();

    ui->de_inTime->setCalendarPopup(true);
    ui->de_outTime->setCalendarPopup(true);
    ui->de_inTime->setDate(QDate::currentDate());
    ui->de_outTime->setDate(QDate::currentDate().addDays(1));
    ui->lb_inTimeWeek->setText("今天");
    QStringList week;
    week << "一" << "二" << "三" << "四" << "五" << "六" << "日";
    GlobalVars::setWeek(week);
    QString day = GlobalVars::week[QDate::currentDate().addDays(1).dayOfWeek()-1];
    ui->lb_outTimeWeek->setText("星期"+day);
    m_inTime = QDate::currentDate().toString("yyyy-MM-dd");
    m_outTime = QDate::currentDate().addDays(1).toString("yyyy-MM-dd");
}

SearchHotelHeader::~SearchHotelHeader()
{
    delete ui;
}

void SearchHotelHeader::setCount(int count)
{
    ui->lb_count->setText(QString::number(count));
}
QString SearchHotelHeader::getInTime()
{
    return m_inTime;
}
QString SearchHotelHeader::getOutTime()
{
    return m_outTime;
}


void SearchHotelHeader::constructeCondition(int type,bool checked,const QString& str)
{
    if (checked)
    {
        switch (type)
        {
            case H_Type:
            {
                m_hotelTypeList.append(str);
                ui->pb_typeNoCondition->setStyleSheet(
                            "min-width: 50px; max-width: 50px;min-height: 24px;max-height: 24px;"
                            "border-radius: 10px;"
                            "background-color: transparent;");
            } break;

            case H_Star:
            {
                QString star = str.split("/").join("-");
                m_hotelStarList.append(star);
                ui->pb_starNoCondition->setStyleSheet(
                            "min-width: 50px; max-width: 50px;min-height: 24px;max-height: 24px;"
                            "border-radius: 10px;"
                            "background-color: transparent;");
            } break;
            default: break;
        }
    }
    else
    {
        switch (type)
        {
            case H_Type:
                if ( m_hotelTypeList.size() > 0)
                {
                      m_hotelTypeList.removeOne(str);
                      if (m_hotelTypeList.size() == 0)
                      {
                          on_pb_typeNoCondition_clicked();
                      }
                }
                 break;
            case H_Star:
                if ( m_hotelStarList.size() > 0)
                {
                    m_hotelStarList.removeOne(str.split("/").join("-"));
                    if (m_hotelStarList.size() == 0)
                    {
                        on_pb_starNoCondition_clicked();
                    }
                }
                 break;
         default: break;
        }
    }
}
void SearchHotelHeader::constructePriceCondition(bool checked,const QString& price)
{
    if (checked)
    {
        ui->pb_priceNoCondition->setStyleSheet(
                    "min-width: 50px; max-width: 50px;min-height: 24px;max-height: 24px;"
                    "border-radius: 10px;"
                    "background-color: transparent;");
        m_hotelPrice  = price;
    }
    else{
        m_hotelPrice = "";
        on_pb_priceNoCondition_clicked();
    }
}
void SearchHotelHeader::constructeMarkCondition(bool checked,const QString& mark, const QString& text)
{
    if (checked)
    {
        ui->pb_markNoCondition->setStyleSheet(
                    "min-width: 50px; max-width: 50px;min-height: 24px;max-height: 24px;"
                    "border-radius: 10px;"
                    "background-color: transparent;");
        m_hotelMark  = mark + "/" + text;
    }
    else{
        m_hotelMark = "";
        on_pb_markNoCondition_clicked();
    }
}

void SearchHotelHeader::on_pb_typeNoCondition_clicked()
{
    ui->pb_typeNoCondition->setStyleSheet(
                "min-width: 50px; max-width: 50px;min-height: 24px;max-height: 24px;"
                "border-radius: 10px;"
                "background-color: #13d1be;");

    if (m_hotelTypeList.size() == 0)
    {
        return;
    }
    m_hotelTypeList.clear();
    ui->cb_economyHotel->setCheckState(Qt::Unchecked);
    ui->cb_themeHotel->setCheckState(Qt::Unchecked);
    ui->cb_businessHotel->setCheckState(Qt::Unchecked);
    ui->cb_apartment->setCheckState(Qt::Unchecked);
    ui->cb_villa->setCheckState(Qt::Unchecked);

    ui->cb_inn->setCheckState(Qt::Unchecked);
    ui->cb_luxuryHotel->setCheckState(Qt::Unchecked);
    ui->cb_hostel->setCheckState(Qt::Unchecked);
    ui->cb_resortHotel->setCheckState(Qt::Unchecked);
    ui->cb_country->setCheckState(Qt::Unchecked);
}
void SearchHotelHeader::on_pb_starNoCondition_clicked()
{
    ui->pb_starNoCondition->setStyleSheet(
                "min-width: 50px; max-width: 50px;min-height: 24px;max-height: 24px;"
                "border-radius: 10px;"
                "background-color: #13d1be;");

    if (m_hotelStarList.size() == 0)
    {
        return;
    }
    m_hotelStarList.clear();
    ui->cb_oneStar->setCheckState(Qt::Unchecked);
    ui->cb_twoStar->setCheckState(Qt::Unchecked);
    ui->cb_threeStar->setCheckState(Qt::Unchecked);
    ui->cb_fourStar->setCheckState(Qt::Unchecked);
    ui->cb_fiveStar->setCheckState(Qt::Unchecked);
}
void SearchHotelHeader::on_pb_priceNoCondition_clicked()
{
    ui->pb_priceNoCondition->setStyleSheet(
                "min-width: 50px; max-width: 50px;min-height: 24px;max-height: 24px;"
                "border-radius: 10px;"
                "background-color: #13d1be;");

    m_hotelPrice = "";
    ui->cb_100_200->setCheckState(Qt::Unchecked);
    ui->cb_200_300->setCheckState(Qt::Unchecked);
    ui->cb_300_400->setCheckState(Qt::Unchecked);
    ui->cb_400_500->setCheckState(Qt::Unchecked);
    ui->cb_below100->setCheckState(Qt::Unchecked);
    ui->cb_greater500->setCheckState(Qt::Unchecked);

    ui->le_lowPrice->clear();
    ui->le_highPrice->clear();
}
void SearchHotelHeader::on_pb_markNoCondition_clicked()
{
    ui->pb_markNoCondition->setStyleSheet(
                "min-width: 50px; max-width: 50px;min-height: 24px;max-height: 24px;"
                "border-radius: 10px;"
                "background-color: #13d1be;");

    m_hotelMark = "";
    ui->cb_greaterFive->setCheckState(Qt::Unchecked);
    ui->cb_greaterFourEight->setCheckState(Qt::Unchecked);
    ui->cb_greaterFourFive->setCheckState(Qt::Unchecked);
    ui->cb_greaterFourTwo->setCheckState(Qt::Unchecked);
    ui->cb_greaterFour->setCheckState(Qt::Unchecked);
    ui->cb_lesserFour->setCheckState(Qt::Unchecked);
}

void SearchHotelHeader::on_de_inTime_userDateChanged(const QDate &date)
{
    if (date <= QDate::currentDate())
    {
        ui->de_inTime->setDate(QDate::currentDate());
        m_inTime = QDate::currentDate().toString("yyyy-MM-dd");
        ui->lb_inTimeWeek->setText("今天");
        return;
    }
    m_inTime = date.toString("yyyy-MM-dd");
    ui->lb_inTimeWeek->setText("星期"+GlobalVars::week[date.dayOfWeek()-1]);
}
void SearchHotelHeader::on_de_outTime_userDateChanged(const QDate &date)
{
    if (date < QDate::currentDate().addDays(1))
    {
        ui->de_outTime->setDate(QDate::currentDate().addDays(1));
        m_outTime = QDate::currentDate().addDays(1).toString("yyyy-MM-dd");
        return;
    }
    m_outTime = date.toString("yyyy-MM-dd");
    ui->lb_outTimeWeek->setText("星期"+GlobalVars::week[date.dayOfWeek()-1]);
}

//////////////////// type condition //////////////////////
void SearchHotelHeader::on_cb_economyHotel_clicked(bool checked)
{
    constructeCondition(H_Type,checked,ui->cb_economyHotel->text());
}
void SearchHotelHeader::on_cb_themeHotel_clicked(bool checked)
{
    constructeCondition(H_Type,checked,ui->cb_themeHotel->text());
}
void SearchHotelHeader::on_cb_hostel_clicked(bool checked)
{
    constructeCondition(H_Type,checked,ui->cb_hostel->text());
}
void SearchHotelHeader::on_cb_resortHotel_clicked(bool checked)
{
    constructeCondition(H_Type,checked,ui->cb_resortHotel->text());
}
void SearchHotelHeader::on_cb_villa_clicked(bool checked)
{
    constructeCondition(H_Type,checked,ui->cb_villa->text());
}
void SearchHotelHeader::on_cb_country_clicked(bool checked)
{
    constructeCondition(H_Type,checked,ui->cb_country->text());
}
void SearchHotelHeader::on_cb_inn_clicked(bool checked)
{
    constructeCondition(H_Type,checked,ui->cb_inn->text());
}
void SearchHotelHeader::on_cb_luxuryHotel_clicked(bool checked)
{
    constructeCondition(H_Type,checked,ui->cb_luxuryHotel->text());
}
void SearchHotelHeader::on_cb_apartment_clicked(bool checked)
{
    constructeCondition(H_Type,checked,ui->cb_apartment->text());
}
void SearchHotelHeader::on_cb_businessHotel_clicked(bool checked)
{
    constructeCondition(H_Type,checked,ui->cb_businessHotel->text());
}

//////////////////// price condition //////////////////////
void SearchHotelHeader::on_cb_below100_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_100_200->setCheckState(Qt::Unchecked);
        ui->cb_200_300->setCheckState(Qt::Unchecked);
        ui->cb_300_400->setCheckState(Qt::Unchecked);
        ui->cb_400_500->setCheckState(Qt::Unchecked);
        ui->cb_greater500->setCheckState(Qt::Unchecked);
    }
    constructePriceCondition(checked,"0-100");
}
void SearchHotelHeader::on_cb_100_200_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_below100->setCheckState(Qt::Unchecked);
        ui->cb_200_300->setCheckState(Qt::Unchecked);
        ui->cb_300_400->setCheckState(Qt::Unchecked);
        ui->cb_400_500->setCheckState(Qt::Unchecked);
        ui->cb_greater500->setCheckState(Qt::Unchecked);
        ui->le_highPrice->clear();
        ui->le_lowPrice->clear();
    }

    constructePriceCondition(checked,"100-200");
}
void SearchHotelHeader::on_cb_200_300_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_below100->setCheckState(Qt::Unchecked);
        ui->cb_100_200->setCheckState(Qt::Unchecked);
        ui->cb_300_400->setCheckState(Qt::Unchecked);
        ui->cb_400_500->setCheckState(Qt::Unchecked);
        ui->cb_greater500->setCheckState(Qt::Unchecked);
        ui->le_highPrice->clear();
        ui->le_lowPrice->clear();
    }

    constructePriceCondition(checked,"200-300");
}
void SearchHotelHeader::on_cb_300_400_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_below100->setCheckState(Qt::Unchecked);
        ui->cb_100_200->setCheckState(Qt::Unchecked);
        ui->cb_200_300->setCheckState(Qt::Unchecked);
        ui->cb_400_500->setCheckState(Qt::Unchecked);
        ui->cb_greater500->setCheckState(Qt::Unchecked);
        ui->le_highPrice->clear();
        ui->le_lowPrice->clear();
    }

    constructePriceCondition(checked,"300-400");
}
void SearchHotelHeader::on_cb_400_500_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_below100->setCheckState(Qt::Unchecked);
        ui->cb_100_200->setCheckState(Qt::Unchecked);
        ui->cb_200_300->setCheckState(Qt::Unchecked);
        ui->cb_300_400->setCheckState(Qt::Unchecked);
        ui->cb_greater500->setCheckState(Qt::Unchecked);
        ui->le_highPrice->clear();
        ui->le_lowPrice->clear();
    }

    constructePriceCondition(checked,"400-500");
}
void SearchHotelHeader::on_cb_greater500_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_below100->setCheckState(Qt::Unchecked);
        ui->cb_100_200->setCheckState(Qt::Unchecked);
        ui->cb_200_300->setCheckState(Qt::Unchecked);
        ui->cb_300_400->setCheckState(Qt::Unchecked);
        ui->cb_400_500->setCheckState(Qt::Unchecked);
        ui->le_highPrice->clear();
        ui->le_lowPrice->clear();
    }

    constructePriceCondition(checked,"500-");
}
void SearchHotelHeader::on_le_lowPrice_editingFinished()
{
    QString highPrice = ui->le_highPrice->text();
    QString lowPrice = ui->le_lowPrice->text();
    if (!highPrice.isEmpty() && !lowPrice.isEmpty())
    {
        if (highPrice.toInt() > lowPrice.toInt())
        {
            ui->cb_100_200->setCheckState(Qt::Unchecked);
            ui->cb_200_300->setCheckState(Qt::Unchecked);
            ui->cb_300_400->setCheckState(Qt::Unchecked);
            ui->cb_400_500->setCheckState(Qt::Unchecked);
            ui->cb_below100->setCheckState(Qt::Unchecked);
            ui->cb_greater500->setCheckState(Qt::Unchecked);
            constructePriceCondition(true,lowPrice + "-" + highPrice);
        }else
        {
            QMessageBox::warning(this,
                         "错误",
                         "请填写正确的价格范围",
                         QMessageBox::Yes,
                         QMessageBox::Yes);
        }
    }
}
void SearchHotelHeader::on_le_highPrice_editingFinished()
{
    on_le_lowPrice_editingFinished();
}
void SearchHotelHeader::on_le_lowPrice_textChanged(const QString &arg1)
{
    if (!arg1.isEmpty())
    {
        QRegExp reg_exp("^[0-9]{1,5}$");
        QRegExpValidator *validator = new QRegExpValidator(reg_exp);
        if(!validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            m_hotelPrice.clear();
            ui->le_lowPrice->setText("");
            ui->le_lowPrice->setPlaceholderText("输入格式错误");
        }
    }
}
void SearchHotelHeader::on_le_highPrice_textChanged(const QString &arg1)
{
    if (!arg1.isEmpty())
    {
        QRegExp reg_exp("^[0-9]{1,5}$");
        QRegExpValidator *validator = new QRegExpValidator(reg_exp);
        if(!validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            m_hotelPrice.clear();
            ui->le_highPrice->setText("");
            ui->le_highPrice->setPlaceholderText("error");
        }
    }
}

//////////////////// mark condition //////////////////////
void SearchHotelHeader::on_cb_greaterFive_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_greaterFourEight->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourFive->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourTwo->setCheckState(Qt::Unchecked);
        ui->cb_greaterFour->setCheckState(Qt::Unchecked);
        ui->cb_lesserFour->setCheckState(Qt::Unchecked);
    }
    constructeMarkCondition(checked,QString::number(HMG_5_0),"5.0");
}
void SearchHotelHeader::on_cb_greaterFourEight_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_greaterFive->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourFive->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourTwo->setCheckState(Qt::Unchecked);
        ui->cb_greaterFour->setCheckState(Qt::Unchecked);
        ui->cb_lesserFour->setCheckState(Qt::Unchecked);
    }
    constructeMarkCondition(checked,QString::number(HMG_4_8),"4.8");
}
void SearchHotelHeader::on_cb_greaterFourFive_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_greaterFive->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourEight->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourTwo->setCheckState(Qt::Unchecked);
        ui->cb_greaterFour->setCheckState(Qt::Unchecked);
        ui->cb_lesserFour->setCheckState(Qt::Unchecked);
    }
    constructeMarkCondition(checked,QString::number(HMG_4_5),"4.5");
}
void SearchHotelHeader::on_cb_greaterFourTwo_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_greaterFive->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourEight->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourFive->setCheckState(Qt::Unchecked);
        ui->cb_greaterFour->setCheckState(Qt::Unchecked);
        ui->cb_lesserFour->setCheckState(Qt::Unchecked);
    }
    constructeMarkCondition(checked,QString::number(HMG_4_2),"4.2");
}
void SearchHotelHeader::on_cb_greaterFour_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_greaterFive->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourEight->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourFive->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourTwo->setCheckState(Qt::Unchecked);
        ui->cb_lesserFour->setCheckState(Qt::Unchecked);
    }
    constructeMarkCondition(checked,QString::number(HMG_4_0),"4.0");
}
void SearchHotelHeader::on_cb_lesserFour_clicked(bool checked)
{
    if (checked)
    {
        ui->cb_greaterFive->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourEight->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourFive->setCheckState(Qt::Unchecked);
        ui->cb_greaterFourTwo->setCheckState(Qt::Unchecked);
        ui->cb_greaterFour->setCheckState(Qt::Unchecked);
    }
    constructeMarkCondition(checked,QString::number(HML_4_0),"4.0");
}

//////////////////// star condition //////////////////////
void SearchHotelHeader::on_cb_oneStar_clicked(bool checked)
{
    constructeCondition(H_Star,checked,ui->cb_oneStar->text());
}
void SearchHotelHeader::on_cb_twoStar_clicked(bool checked)
{
    constructeCondition(H_Star,checked,ui->cb_twoStar->text());
}
void SearchHotelHeader::on_cb_threeStar_clicked(bool checked)
{
    constructeCondition(H_Star,checked,ui->cb_threeStar->text());
}
void SearchHotelHeader::on_cb_fourStar_clicked(bool checked)
{
    constructeCondition(H_Star,checked,ui->cb_fourStar->text());
}
void SearchHotelHeader::on_cb_fiveStar_clicked(bool checked)
{
    constructeCondition(H_Star,checked,ui->cb_fiveStar->text());
}

void SearchHotelHeader::on_le_city_editingFinished()
{
    if (!ui->le_city->text().isEmpty())
    {
        m_city = ui->le_city->text();
    }
    else{
        m_city.clear();
    }
}
void SearchHotelHeader::on_le_city_textChanged(const QString &arg1)
{
    if (!arg1.isEmpty())
    {
        QRegExp reg_exp("^\\w+$");
        QRegExpValidator *validator = new QRegExpValidator(reg_exp);
        if(!validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            m_city.clear();
            ui->le_city->setText("");
            ui->le_city->setPlaceholderText("格式错误");
        }
        else
        {
            ui->le_city->setPlaceholderText("入住城市");
        }
    }
}
void SearchHotelHeader::on_le_condition_editingFinished()
{
    if (!ui->le_condition->text().isEmpty())
    {
        m_locationOrName = ui->le_condition->text();
    }
    else
    {
        m_locationOrName.clear();
    }
}
void SearchHotelHeader::on_le_condition_textChanged(const QString &arg1)
{
    if (!arg1.isEmpty())
    {
        QRegExp reg_exp("^\\w+$");
        QRegExpValidator *validator = new QRegExpValidator(reg_exp);
        if(!validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            m_locationOrName.clear();
            ui->le_condition->setText("");
            ui->le_condition->setPlaceholderText("输入格式错误");
        }else
            {
            ui->le_condition->setPlaceholderText("（关键词选填）酒店名/位置");
        }
    }
}


void SearchHotelHeader::on_pb_search_clicked()
{
    GlobalVars::count = 0;
    qDebug() << "hotelTypelList = " << m_hotelTypeList;
    qDebug() << "hotelStarlList = " << m_hotelStarList;
    qDebug() << m_hotelPrice << m_hotelMark << m_city;
    qDebug() << m_locationOrName<< m_inTime << m_outTime;

    QString hotelType = m_hotelTypeList.join("/");
    QString hotelStar = m_hotelStarList.join("/");

    QString msg = QString(CMD_HotelSearch_S)
        % QString("#") % GlobalVars::g_localUser.getNumber()
            % "|" % "" % "/" % ""
            % "|" % QString::number(H_Type) % "/" % hotelType
            % "|" % QString::number(H_Star) % "/" % hotelStar
            % "|" % QString::number(H_Mark) % "/" % m_hotelMark
            % "|" % QString::number(H_City) % "/" % m_city
            % "|" % QString::number(H_NameOrLocation) % "/" % m_locationOrName
            % "|" % QString::number(H_Price) % "/" % m_hotelPrice;
//            % "|" % QString::number(H_Location) % "/" % m_locationOrName;
//            % "|" % QString::number(H_InTime) % "/" % m_inTime
//            % "|" % QString::number(H_OutTime) % "/" % m_outTime;

    emit signalGainHotelInfoList(msg);
}

